<template>
  <div>
    <el-form ref="sRef" :inline="true" :model="dictValueCl.dictValueS" class="demo-dynamic">
      <el-form-item label="字典类型名称：" prop="dictKey">
        <el-input v-model="dictValueCl.dictValueS.dictKey" placeholder="字典编码" type="text"></el-input>
      </el-form-item>
      <el-form-item label="字典值名称：" prop="dictName">
        <el-input v-model="dictValueCl.dictValueS.dictName" placeholder="字典名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="字典值：" prop="dictValue">
        <el-input v-model="dictValueCl.dictValueS.dictValue" placeholder="字典值" type="text"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(sRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加字典
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="dictValueCl.tableData" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (dictValueCl.dictValueS.page - 1) * dictValueCl.dictValueS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="字典类型" prop="dictKeyName" width="180" />
      <el-table-column align="center" header-align="center" label="字典名称" prop="dictName" width="180" />
      <el-table-column align="center" header-align="center" label="字典值" prop="dictValue" width="180" />
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteDictValue(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dictValueCl.dialogDictValue" destroy-on-close title="添加字典类" @close="resetE(dictValueEditRef)">
      <el-form ref="dictValueEditRef" :model="dictValueCl.dictValueEdit" :rules="dictValueCl.rules">
        <el-form-item label="字典类型" prop="keyId">
          <el-select v-model="dictValueCl.dictValueEdit.keyId" placeholder="请选择字典类型">
            <el-option v-for="item in dictValueCl.selectDictKey" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="dictValueCl.dictValueEdit.dictName" type="text"></el-input>
        </el-form-item>
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="dictValueCl.dictValueEdit.dictValue" type="text"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dictValueCl.dictValueEdit.sort" :min="1" />
        </el-form-item>
        <el-form-item autocomplete="off" label="备注" prop="remark">
          <Editor v-model:editorValue="dictValueCl.dictValueEdit.remark"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存 </el-button>
          <el-button @click="dictValueCl.dialogDictValue = false">取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <div class="pagination-block">
    <Page :pageData="dictValueCl.dictValueS" @selectPage="search"></Page>
  </div>
</template>
<script lang="ts" setup>
import { ElMessage, FormInstance } from 'element-plus/es'
import { delDictValue, detailDictValue, pageDictValue, saveDictValue, selectDictKey, updateDictValue } from '@/api/system'
import Editor from '@/components/MyEditor.vue'
import { baseListInfo } from '@/page/types/base/baseList'
import { dictValueClass, dictValueInfo } from '@/page/types/system/dict/value'

const dictValueCl = reactive(new dictValueClass())
const dictValueEditRef = ref()
const sRef = ref()
const search = () => {
  pageDictValue(dictValueCl.dictValueS).then(res => {
    dictValueCl.tableData = res.data.records as dictValueInfo[]
    dictValueCl.dictValueS.page = res.data.current
    dictValueCl.dictValueS.size = res.data.size
    dictValueCl.dictValueS.total = res.data.total
  })
}
const resetS = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  search()
}
const opendialog = () => {
  dictValueCl.dialogDictValue = true
  selectDictKey(null).then(res => {
    dictValueCl.selectDictKey = res.data as baseListInfo[]
  })
}
const edit = (id: string) => {
  detailDictValue(id).then(res => {
    dictValueCl.dialogDictValue = true
    dictValueCl.dictValueEdit = res.data as dictValueInfo
  })
  selectDictKey(null).then(res => {
    dictValueCl.selectDictKey = res.data as baseListInfo[]
  })
}
const deleteDictValue = (id: string) => {
  delDictValue(id).then(res => {
    search()
    ElMessage({
      message: '删除字典类型信息成功',
      type: 'success'
    })
  })
}
const resetE = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  dictValueCl.dictValueEdit = {} as dictValueInfo
}
const save = () => {
  if (dictValueCl.dictValueEdit.id) {
    updateDictValue(dictValueCl.dictValueEdit).then(res => {
      search()
      ElMessage({
        message: '更新字典类型信息成功',
        type: 'success'
      })
    })
  } else {
    saveDictValue(dictValueCl.dictValueEdit).then(res => {
      search()
      ElMessage({
        message: '保存字典类型信息成功',
        type: 'success'
      })
    })
  }
  dictValueCl.dialogDictValue = false
}
onMounted(() => {
  search()
})
</script>
<style lang="scss" scoped>
.pagination-block {
  position: relative;

  .el-pagination {
    margin-top: 20px;
    position: absolute;
    right: 0%;
  }
}
.el-select {
  width: 200px;
}
</style>
